﻿using System;
using System.Web;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity
{
	public class SiteContext
	{
		private const Double COOKIEEXPIRES = 4;
		public const string CONST_USERSESSION = "HealthCommunity.User";
		public const string CONST_USERSESSIONCOOKIE = "HealthCommunity.UserIDCookie";
		public const string CONST_DRUGSTOREID = "HealthCommunity.DrugStoreID";
		private HttpContext _context = HttpContext.Current;

		private User _CurrentUser;

		private SiteContext()
		{
		}

		public static SiteContext Current
		{
			get
			{
				if (HttpContext.Current == null)
				{
					return null;
				}

				if (HttpContext.Current.Items["SiteContext"] == null)
				{
					SiteContext context = new SiteContext();
					HttpContext.Current.Items.Add("SiteContext", context);

					return context;
				}

				return (SiteContext)HttpContext.Current.Items["SiteContext"];
			}
		}

		/// <summary>
		/// Gets an user host address
		/// </summary>
		public string UserHostAddress
		{
			get
			{
				if (HttpContext.Current != null &&
						HttpContext.Current.Request != null &&
						HttpContext.Current.Request.UserHostAddress != null)
					return HttpContext.Current.Request.UserHostAddress;
				else
					return string.Empty;
			}
		}

		public User CurrentUser
		{
			get
			{
				if (_CurrentUser == null)
				{
					if ((HttpContext.Current.Request.Cookies[CONST_USERSESSIONCOOKIE] != null) && !(string.IsNullOrEmpty(HttpContext.Current.Request.Cookies[CONST_USERSESSIONCOOKIE].Value)))
					{
						var userID = 0;
						if (int.TryParse(HttpContext.Current.Request.Cookies[CONST_USERSESSIONCOOKIE].Value, out userID))
						{
							var userProvider = new UserProvider();

							this._CurrentUser = userProvider.GetUser(userID, true);
						}
					}
				}

				return this._CurrentUser;
			}
			set
			{
				this._CurrentUser = value;
			}
		}

		public DrugStore CurrentDrugStore
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets an object item in the context by the specified key.
		/// </summary>
		/// <param name="key">The key of the value to get.</param>
		/// <returns>The value associated with the specified key.</returns>
		public object this[string key]
		{
			get
			{
				if (this._context == null)
				{
					return null;
				}

				if (this._context.Items[key] != null)
				{
					return this._context.Items[key];
				}

				return null;
			}
			set
			{
				if (this._context != null)
				{
					this._context.Items.Remove(key);
					this._context.Items.Add(key, value);
				}
			}
		}

		/// <summary>
		/// Sets cookie
		/// </summary>
		/// <param name="application">Application</param>
		/// <param name="key">Key</param>
		/// <param name="val">Value</param>
		public static void SetCookie(HttpApplication application, string key, string val)
		{
			HttpCookie cookie = new HttpCookie(key);
			cookie.Value = val;
			if (string.IsNullOrEmpty(val))
			{
				cookie.Expires = DateTime.Now.AddMonths(-1);
			}
			else
			{
				cookie.Expires = DateTime.Now.AddHours(COOKIEEXPIRES);
			}
			application.Response.Cookies.Remove(key);
			application.Response.Cookies.Add(cookie);
		}

		public static string GetCookie(string key)
		{
			var result = string.Empty;

			if(HttpContext.Current.Request.Cookies[key] != null && !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies[key].Value))
			{
				result = HttpContext.Current.Request.Cookies[CONST_DRUGSTOREID].Value;
			}

			return result;
		}
	}
}